<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>微博详情</h1>
<div id="v_div">
   <h2>{{weibo.author}}说{{weibo.content}}</h2>
    <p>发布于:{{weibo.created}}</p>
    <img :src="weibo.url" style="width: 500px" alt="">
    <h3>评论相关</h3>
    <div v-for="comment in arr">
        <h4>{{comment.author}}说:{{comment.content}}</h4>
        <p>发布于:{{comment.created}}</p>
    </div>
    <form action="">
        <input type="text" name="content" placeholder="评论">
        <input type="button" value="评论" @click="comment()">

    </form>
</div>
<!--引入vue框架-->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
    let v=new Vue({
        el:"#v_div",
        data:{
            weibo:{},
            arr:[]
        },
        created:function () {
            //得到weibo的id
            let id=location.search.split("=")[1]
            axios.get("/selectById?id="+id).then(function (response) {
                v.weibo=response.data;

            })
            //发请求获取当前微博相关的所有评论
            axios.get("/selectComment?id="+id).then(function (response) {
                v.arr=response.data;

            })
        },
        methods:{
            comment:function () {
                let data=new FormData(document.querySelector("form"))
                //得到地址栏中的微博id 并添加到data对象中
                let id=location.search.split("=")[1]
                data.set("weiboId",id)
                axios.post("/comment",data).then(function (response) {
                    if (response.data==1){
                        alert("评论成功")
                        location.reload();
                    }else {
                        alert("先登入")
                        location.href="login.html"
                    }

                })
            }
        }
    })
</script>
</body>
</html>